package com.sihan.swvp.sync.task;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.sihan.swvp.common.CommonUtil;
import com.sihan.swvp.service.sys.SyncService;
import com.sihan.swvp.sync.Synchronizer;

/**
 * 数据映射抽象类
 * @author jch
 *
 */
public abstract class TaskSynchronizer extends Synchronizer{
	//原始字段
	public String[] rawfields;
	//新字段，其中原始字段和新字段必须长度一致
	public String[] datafields;
	//最大同步天数
	public int maxdays = 1;
	//同步数据目标表
	public String datatable;
	//目标表关键字
	public String datakey;
	//参数
	public Map<String,String> params;
	
	@Autowired
    SyncService syncService;
	
	/**
	 * 数据映射
	 * @param raw
	 * @return
	 */
	public Map<String,Object> transfer(Map<String,Object> raw){
		Map<String,Object> data = new HashMap<String,Object>();
		for (int i = 0; i < rawfields.length; i++) {
			data.put(datafields[i], raw.get(rawfields[i]));
		}
		
		return data;
	}
	
	/**
	 * 获取最后同步时间
	 * @return
	 */
	public String getLastTime(){
		String time = syncService.getLastSyncTime(jobid);
		if(null == time){
			Date date = CommonUtil.dateAdd(new Date(), Calendar.DATE, -maxdays);
			time = CommonUtil.date2string(date);
		}
		
		return time;
	}
	
	/**
	 * 获取最后同步时间
	 * @return
	 */
	public Date getLastDate(){
		String time = syncService.getLastSyncTime(jobid);
		if(null == time){
			Date date = CommonUtil.dateAdd(new Date(), Calendar.DATE, -maxdays);
			
			return date;
		}
		
		return CommonUtil.o2date(time);
	}
	


	public Map<String, String> getParams() {
		return params;
	}


	public void setParams(Map<String, String> params) {
		this.params = params;
	}


	public SyncService getSyncService() {
		return syncService;
	}


	public void setSyncService(SyncService syncService) {
		this.syncService = syncService;
	}


	public int getMaxdays() {
		return maxdays;
	}

	public void setMaxdays(int maxdays) {
		this.maxdays = maxdays;
	}



	public String[] getRawfields() {
		return rawfields;
	}
	public void setRawfields(String[] rawfields) {
		this.rawfields = rawfields;
	}
	public String[] getDatafields() {
		return datafields;
	}
	public void setDatafields(String[] datafields) {
		this.datafields = datafields;
	}
}
